// pages/volunteer/my-activities.js
Page({
  data: {
    userActivities: []
  },

  onLoad: function (options) {
    console.log('我的活动页面加载');
  },

  onShow: function () {
    this.loadUserActivities();
  },

  // 加载用户活动数据
  loadUserActivities: function () {
    const userActivities = wx.getStorageSync('userActivities') || [];
    
    // 为每个活动添加积分信息（模拟）
    const activitiesWithPoints = userActivities.map(activity => ({
      ...activity,
      points: Math.floor(Math.random() * 50) + 10 // 随机积分10-60分
    }));
    
    this.setData({
      userActivities: activitiesWithPoints
    });
    
    console.log('用户活动数据加载完成:', activitiesWithPoints);
  },

  // 获取状态文本
  getStatusText: function (status) {
    const statusMap = {
      'registered': '已报名',
      'confirmed': '已确认',
      'completed': '已完成',
      'cancelled': '已取消'
    };
    return statusMap[status] || '未知状态';
  },

  // 格式化时间
  formatTime: function (timeString) {
    const date = new Date(timeString);
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');
    
    return `${year}-${month}-${day} ${hours}:${minutes}`;
  },

  // 查看活动详情
  viewActivityDetail: function (e) {
    const activityId = e.currentTarget.dataset.id;
    
    wx.navigateTo({
      url: `/pages/volunteer/activity-detail?id=${activityId}`,
      fail: (err) => {
        console.error('页面跳转失败:', err);
        wx.showToast({
          title: '页面跳转失败',
          icon: 'none'
        });
      }
    });
  },

  // 取消活动报名
  cancelActivity: function (e) {
    const activityId = e.currentTarget.dataset.id;
    const activity = this.data.userActivities.find(item => item.activityId === activityId);
    
    if (!activity) return;
    
    wx.showModal({
      title: '取消报名',
      content: `确定要取消报名"${activity.activityTitle}"吗？`,
      confirmText: '确定取消',
      cancelText: '再想想',
      success: (res) => {
        if (res.confirm) {
          this.performCancelActivity(activityId);
        }
      }
    });
  },

  // 执行取消活动
  performCancelActivity: function (activityId) {
    wx.showLoading({
      title: '取消中...'
    });

    setTimeout(() => {
      // 更新本地存储中的活动状态
      const userActivities = wx.getStorageSync('userActivities') || [];
      const updatedActivities = userActivities.map(activity => {
        if (activity.activityId === activityId) {
          return { ...activity, status: 'cancelled' };
        }
        return activity;
      });
      
      wx.setStorageSync('userActivities', updatedActivities);
      
      // 更新页面数据
      this.loadUserActivities();
      
      wx.hideLoading();
      wx.showToast({
        title: '取消成功',
        icon: 'success'
      });
    }, 1000);
  },

  // 跳转到活动列表
  goToActivities: function () {
    wx.navigateTo({
      url: '/pages/volunteer/activities',
      fail: (err) => {
        console.error('页面跳转失败:', err);
        wx.showToast({
          title: '页面跳转失败',
          icon: 'none'
        });
      }
    });
  },

  // 下拉刷新
  onPullDownRefresh: function () {
    this.loadUserActivities();
    setTimeout(() => {
      wx.stopPullDownRefresh();
      wx.showToast({
        title: '刷新成功',
        icon: 'success'
      });
    }, 1000);
  },

  // 分享
  onShareAppMessage: function () {
    return {
      title: '我的活动 - 志愿者活动',
      path: '/pages/volunteer/my-activities'
    };
  }
});
